package com.qzsf.backend.mapper;

import com.qzsf.backend.dto.RegistrationDetailDTO;
import com.qzsf.backend.entity.Event;
import com.qzsf.backend.entity.Registration;
import com.qzsf.backend.mapper.sql.RegistrationSqlProvider;
import com.qzsf.backend.vo.RegistrationBriefVO;
import com.qzsf.backend.vo.UserBriefVO;
import com.qzsf.backend.vo.UserEventVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/*
 * @Author: Dennis Chen
 * @CreateTime: 2024/11/21 8:56 周四
 * @Email 1429278462@qq.com
 * @Remark 报名数据访问层
 * @Version 1.0
 */
@Mapper
@Repository
public interface RegistrationMapper {
    @SelectProvider(type = RegistrationSqlProvider.class, method = "getRegistrationByCondition")
    List<RegistrationDetailDTO> getRegistration(Integer status, String userName, String eventName);

    // 批量更新状态为通过
    void batchApprove(@Param("ids") List<Integer> ids);

    //批量更新状态为拒绝
    void batchReject(@Param("ids") List<Integer> ids);

    //统计活动中已审批的报名人数
    @Select("SELECT COUNT(*) FROM registration WHERE event_id = #{eventId} AND status = 1")
    int countApprovedByEventId(@Param("eventId") Integer eventId);

    //根据报名ID查询对应的活动ID
    List<Integer> getEventIdsByRegistrationIds(@Param("list") List<Integer> ids);

    //根据活动ID获取所有报名成功的用户ID
    @Select("SELECT user_id FROM registration WHERE event_id = #{eventId} AND status = 1")
    List<Integer> findUserIdsByEventId(@Param("eventId") Integer eventId);

    @Select("select b.* from registration a left join event b on a.event_id = b.id where a.user_id = #{id} and a.status = 1 ORDER BY a.created_at DESC")
    List<Event> getUserEvent(int id);

    @Select("select * from registration where user_id = #{id} and event_id = #{eventId}")
    Registration getRegistrationByUserAndEvent(int id,int eventId);

    @Insert("insert into registration (user_id, event_id, status) values (#{userId},#{eventId},#{status})")
    int createRegistration(Registration registration);

    @Select("select count(*) from registration where event_id = #{eventId} and status = 1")
    int getEventRegistrationNum(int eventId);

    @Select("select b.id,b.name,b.pic from registration a left join user_profile b on a.user_id = b.id where event_id = #{eventId} and status = 1")
    List<UserBriefVO> getEventRegisteredUser(int eventId);

    @Select("select a.id,b.name as userName,c.name as eventName,b.pic from registration a left join user_profile b on a.user_id = b.id left join event c on a.event_id = c.id  where a.event_id = #{eventId} and a.status = 0")
    List<RegistrationBriefVO> getEventUncheckedRegistration(int eventId);

    @Update("update registration set status = 2 where event_id = #{eventId} and user_id = #{userId}")
    int cancelRegistration(int eventId,int userId);


    //根据报名信息id批量获取用户id
    List<Integer> getUserIdsByRegistrationIds(List<Integer> ids);
}
